package com.fn.flutter_fn_sdk.page;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.fn.flutter_fn_sdk.event.AdErrorEvent;
import com.fn.flutter_fn_sdk.event.AdEvent;
import com.fn.flutter_fn_sdk.event.AdEventAction;
import com.fn.flutter_fn_sdk.event.AdRewardEvent;
import com.fn.sdk.api.reward.FnRewardAd;
import com.fn.sdk.api.reward.FnRewardAdListener;

import io.flutter.plugin.common.MethodCall;

/**
 * 激励视频页面
 */
public class RewardVideoPage extends BaseAdPage {
    private static final String TAG = RewardVideoPage.class.getSimpleName();
    // 设置激励视频服务端验证的自定义信息
    private String customData;
    // 设置服务端验证的用户信息
    private String userId;
    private static RewardVideoPage instance = null;

    public static RewardVideoPage getInstance() {
        if (instance == null) {
            instance = new RewardVideoPage();
        }
        return instance;
    }
    @Override
    public void loadAd(Activity activity, @NonNull MethodCall call) {
        customData = call.argument("customData");
        userId = call.argument("userId");
        this.activity = activity;
        loadAndShowAd();
    }

    /**
     * 激励视频预加载
     */
    public void loadAdOnly(Activity activity, String posId, @NonNull MethodCall call) {
        this.posId = posId;
        customData = call.argument("customData");
        userId = call.argument("userId");
        this.activity = activity;
        loadOnly();
    }


    /**
     * 播放预加载激励视频
     * @return 是否播放成功
     */
    public boolean showAdOnly() {
       return showOnly();
    }


    /**
    * 加载并播放激励视屏
    */
    private void loadAndShowAd(){
        FnRewardAd rewardAd = new FnRewardAd();
        rewardAd.setUserId(userId);
        rewardAd.setExtraInfo(customData);
        rewardAd.loadAd(activity, posId, new FnRewardAdListener() {
            @Override
            public void onError(int code, String s, String detail) {
                Log.e(TAG, "loadAndShowAd:onError adError:" + code+"s:"+s+"s1:"+detail);
                sendErrorEvent(code, s);
            }

            @Override
            public void onLoaded() {
                Log.i(TAG, "loadAndShowAd:onLoaded");
                sendEvent(AdEventAction.onAdLoaded);
            }

            @Override
            public void onCached() {
                Log.i(TAG, "loadAndShowAd:onCached");
            }

            @Override
            public void onShow() {
                Log.i(TAG, "loadAndShowAd:onShow");
                sendEvent(AdEventAction.onAdPresent);
            }

            @Override
            public void onExpose() {
                Log.i(TAG, "loadAndShowAd:onExpose");
                sendEvent(AdEventAction.onAdExposure);
            }

            @Override
            public void onReward(String orderId) {
                Log.i(TAG, "loadAndShowAd:onReward orderId:" + orderId);
                sendEvent(new AdRewardEvent(posId, AdEventAction.onAdReward, orderId, customData, userId));
            }

            @Override
            public void onClick() {
                Log.i(TAG, "loadAndShowAd:onClick");
                sendEvent(AdEventAction.onAdClicked);
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "loadAndShowAd:onComplete");
                sendEvent(AdEventAction.onAdComplete);
            }

            @Override
            public void onClose() {
                Log.i(TAG, "loadAndShowAd:onClose");
                sendEvent(AdEventAction.onAdClosed);
            }
        });
    }

    /**
    * 预加载激励视频
    */
    private void loadOnly() {
        FnRewardAd rewardAd = FnRewardAd.getInstance();
        rewardAd.setUserId(userId);
        rewardAd.setExtraInfo(customData);
        rewardAd.loadOnly(activity, posId, new FnRewardAdListener() {
            @Override
            public void onLoaded() {
                Log.i(TAG, "loadOnly:onLoaded");
                //sendEvent(AdEventAction.onAdLoaded);
                AdEvent event = new AdEvent(posId, AdEventAction.onAdLoaded);
                event.tag = "loadOnly";
                sendEvent(event);
            }

            @Override
            public void onCached() {
                Log.i(TAG, "loadOnly:onCached");
            }

            @Override
            public void onShow() {
                Log.i(TAG, "loadOnly:onShow");
                sendEvent(AdEventAction.onAdPresent);
            }

            @Override
            public void onExpose() {
                Log.i(TAG, "loadOnly:onExposure");
                sendEvent(AdEventAction.onAdExposure);
            }

            @Override
            public void onReward(String orderId) {
                Log.i(TAG, "loadOnly:onReward " + orderId);  // 获取服务端验证的唯一 ID
                sendEvent(new AdRewardEvent(posId, AdEventAction.onAdReward, orderId, customData, userId));
            }

            @Override
            public void onClick() {
                Log.i(TAG, "loadOnly:onClick");
                sendEvent(AdEventAction.onAdClicked);
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "loadOnly:onComplete");
                sendEvent(AdEventAction.onAdComplete);
            }

            @Override
            public void onClose() {
                Log.i(TAG, "loadOnly:onClose");
                sendEvent(AdEventAction.onAdClosed);
            }

            @Override
            public void onError(int code, String message, String detail) {
                Log.d(TAG, "onError adError:" + code+"s:"+message+"s1:"+detail);
//                sendErrorEvent(code, message);
                AdErrorEvent event = new AdErrorEvent(posId, code, message);
                event.tag = "loadOnly";
                sendEvent(event);
            }
        });
    }

    /**
    * 播放预加载视频
    */
    private boolean showOnly(){
        boolean isShow = FnRewardAd.getInstance().showOnly();
        if(!isShow){
            sendErrorEvent(112, "未预加载广告, 请先预加载广告。");
        }
        return isShow;
    }
}
